﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace EmployeeCheckIn
{
    public class UpdateData
    {
        public static object GetDataValue(object value)
        {
            if (value == null|| value == "")
            {
                return DBNull.Value;
            }

            return value;
        }
        public void Update(EmpEntity emp, int i)
        {

            string sqltext = @"UPDATE EMPLOYEE SET    
                                  EMP_CODE = @emp_code
                                 ,EMP_TITLE = @emp_title
                                 ,EMP_FIRSTNAME = @emp_firstname
                                 ,EMP_LASTNAME =  @emp_lastname
                                 ,EMP_POSITION_ID = @emp_position_id
                                 ,EMP_START_DATE = @emp_start_date
                                 ,EMP_BIRTH_DATE = @emp_birth_date
                                 ,EMP_ID_CARD = @emp_id_card
                                 ,EMP_ADDRESS = @emp_address
                                 ,EMP_PROVINCE_ID = @emp_province_id
                                 ,EMP_POST_CODE = @emp_post_code
                                 ,EMP_PHONE = @emp_phone
                                 ,EMP_STATUS_ID = @emp_status_id
                                 ,EMP_PIC = @emp_pic
                                   WHERE EMP_ID = @i";

            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@i", i);
            sqlcomm.Parameters.AddWithValue("@emp_code", GetDataValue(emp.EmpId));
            sqlcomm.Parameters.AddWithValue("@emp_title", GetDataValue(emp.Prefix));
            sqlcomm.Parameters.AddWithValue("@emp_firstname", GetDataValue(emp.FirstName));
            sqlcomm.Parameters.AddWithValue("@emp_lastname", GetDataValue(emp.LastName));
            sqlcomm.Parameters.AddWithValue("@emp_position_id",GetDataValue(emp.Position) );
            sqlcomm.Parameters.AddWithValue("@emp_start_date", GetDataValue(emp.DateStart));
            sqlcomm.Parameters.AddWithValue("@emp_birth_date",GetDataValue(emp.DateBirth) );
            sqlcomm.Parameters.AddWithValue("@emp_id_card", GetDataValue(emp.CardID));
            sqlcomm.Parameters.AddWithValue("@emp_address", GetDataValue(emp.Address));
            sqlcomm.Parameters.AddWithValue("@emp_province_id",GetDataValue(emp.City) );
            sqlcomm.Parameters.AddWithValue("@emp_post_code",GetDataValue(emp.PostID) );
            sqlcomm.Parameters.AddWithValue("@emp_phone", GetDataValue(emp.Phone));
            sqlcomm.Parameters.AddWithValue("@emp_status_id",GetDataValue(emp.status) );
            sqlcomm.Parameters.AddWithValue("@emp_pic",GetDataValue(emp.EmpPic));
            
            
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
    }
}